package com.jiezhuo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiezhuo.entity.Template;
import com.jiezhuo.entity.vo.TemVo;
import com.jiezhuo.handler.RzException;
import com.jiezhuo.service.TemplateService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2022-12-23
 */
@RestController
@Api(tags = "模板管理")
@RequestMapping("/template")
public class TemplateController {
    @Autowired
    TemplateService templateService;
    @ApiOperation("添加或修改模板")
    @PostMapping("saveTemplate")
    public R saveTemplate(@RequestBody TemVo temVo){
//        JSONObject jsonObject = JSON.parseObject(tem);
//        System.out.println(jsonObject);
//        JSONObject data = jsonObject.getJSONObject("data");
//        String tid=jsonObject.getString("tid");
//        String tname=jsonObject.getString("tname");
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);

        Template template = new Template();
        template.setTemplateId(temVo.getTid());
        template.setTemplateName(temVo.getTname());
        template.setData(temVo.getData());
        if(!StringUtils.isEmpty(temVo.getTid())){
            template.setUpdateTime(dateNowStr);
        }else {
            template.setCreateTime(dateNowStr);
            template.setUpdateTime(dateNowStr);
        }
        boolean save =true;
        try {
            templateService.saveOrUpdate(template);
        }catch (Exception e){
            throw new RzException(400,"添加失败");
        }
        return R.ok().data("data",save);
    }
    @ApiOperation("查询模板")
    @PostMapping("getTemplatePage")
    public R getTemplatePage(String id,String name,Integer limit,Integer page){
        QueryWrapper<Template> templateQueryWrapper = new QueryWrapper<>();
        templateQueryWrapper.eq(!StringUtils.isEmpty(id),"template_id",id);
        templateQueryWrapper.like(!StringUtils.isEmpty(name),"template_name",name);
        Page<Template> templatePage = templateService.getBaseMapper().selectPage(new Page<>(page, limit), templateQueryWrapper);
        return R.ok().data("data",templatePage);
    }
    @ApiOperation("查询模板")
    @PostMapping("getTemplate")
    public R getTemplate(){
        List<Template> templates = templateService.getBaseMapper().selectList(null);
        return R.ok().data("data",templates);
    }
    @ApiOperation("删除模板")
    @PostMapping("delInk")
    public R delInk(String id){
        boolean b = templateService.removeById(id);
        if(b){
            return R.ok().msg("删除成功");
        }
        return R.err().msg("删除失败");
    }

}

